package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.DataApiBo;
import com.wande.dataplatform.domain.dto.ApiResponseDTO;
import com.wande.dataplatform.domain.dto.ApiStatisticsDTO;
import com.wande.dataplatform.domain.vo.ApiCallLogVO;
import com.wande.dataplatform.domain.vo.DataApiVO;
import com.wande.dataplatform.service.IDataApiService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 数据服务API控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/dataplatform/data-api")
public class DataApiController extends BaseController {

    private final IDataApiService dataApiService;

    /**
     * 查询数据服务API列表
     */
    @SaCheckPermission("dataplatform:dataApi:list")
    @GetMapping("/list")
    public TableDataInfo<DataApiVO> list(DataApiBo bo, PageQuery pageQuery) {
        return dataApiService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询数据服务API详情
     */
    @SaCheckPermission("dataplatform:dataApi:query")
    @GetMapping("/{id}")
    public R<DataApiVO> getInfo(@PathVariable Long id) {
        return R.ok(dataApiService.queryById(id));
    }

    /**
     * 新增数据服务API
     */
    @SaCheckPermission("dataplatform:dataApi:add")
    @Log(title = "数据服务API", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@Valid @RequestBody DataApiBo bo) {
        return R.ok(dataApiService.insertByBo(bo));
    }

    /**
     * 修改数据服务API
     */
    @SaCheckPermission("dataplatform:dataApi:edit")
    @Log(title = "数据服务API", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody DataApiBo bo) {
        dataApiService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除数据服务API
     */
    @SaCheckPermission("dataplatform:dataApi:remove")
    @Log(title = "数据服务API", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        dataApiService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 发布API
     */
    @SaCheckPermission("dataplatform:dataApi:publish")
    @Log(title = "发布API", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/publish")
    public R<Void> publish(@PathVariable Long id) {
        dataApiService.publishApi(id);
        return R.ok();
    }

    /**
     * 下线API
     */
    @SaCheckPermission("dataplatform:dataApi:offline")
    @Log(title = "下线API", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/offline")
    public R<Void> offline(@PathVariable Long id) {
        dataApiService.offlineApi(id);
        return R.ok();
    }

    /**
     * 测试API
     */
    @SaCheckPermission("dataplatform:dataApi:test")
    @PostMapping("/{id}/test")
    public R<ApiResponseDTO> test(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        return R.ok(dataApiService.testApi(id, params));
    }

    /**
     * 获取API统计信息
     */
    @SaCheckPermission("dataplatform:dataApi:stats")
    @GetMapping("/{id}/statistics")
    public R<ApiStatisticsDTO> getStatistics(
        @PathVariable Long id,
        @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
        @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        return R.ok(dataApiService.getStatistics(id, startDate, endDate));
    }

    /**
     * 查询API调用日志
     */
    @SaCheckPermission("dataplatform:dataApi:logs")
    @GetMapping("/{id}/logs")
    public TableDataInfo<ApiCallLogVO> getLogs(@PathVariable Long id, PageQuery pageQuery) {
        return dataApiService.queryCallLogs(id, pageQuery);
    }

    /**
     * 调用API（公开接口，需要token）
     */
    @PostMapping("/call/{path}/{version}")
    public R<ApiResponseDTO> callApi(
        @PathVariable String path,
        @PathVariable String version,
        @RequestBody Map<String, Object> params,
        @RequestHeader(value = "Authorization", required = false) String token) {
        return R.ok(dataApiService.callApi(path, version, params, token));
    }
}
